<?php

namespace Wap\Controller;

use Think\Controller;

use Wap\Service\PraiseService;
use Wap\Service\WeixinService;
use Wap\Service\RuleService;

class PraiseItemRecordController extends Controller
{
    public function add()//TODO: could be better
    {
        $result = array('error' => -1, 'msg' => '投票失败');
        $session_time = session("mt_time");
        if ($session_time != null && ($session_time + 2) > time()) {
            $result['error'] = 302;//用户状态没有发生变化
            $result['msg'] = "操作过于频繁，请稍后重试！";
            $result['img'] = '';
            return $this->ajaxReturn($result);
        }
        session("mt_time", time());
        $pid = I("post.item", -1, "");
        $praise_id = I("post.praise", -1, "");
        $openid = I('post.openid', '', '');
        $unionid = I('post.union_id', '', '');
        $check = I('post.check', '', '');
        if ($check != $pid . "462" . $openid) {
            $result['error'] = 303;//服务器繁忙--
            $result['msg'] = '服务器繁忙请稍后';
            return $this->ajaxReturn($result);
        }
        // $is_good =	WeixinService::is_good_openid( $openid, $appid, $appsecret );//查看是否是粉丝
        $praise = PraiseService::getPraise($praise_id);
        $wxuser = WeixinService::getWxuser($praise['token']);
        $user = WeixinService::getUser();
        $is_follow = WeixinService::getFollowStatus($user, $wxuser, $unionid, $praise['showpic']);
        // trace( $is_follow, '用户状态没有发生变化', 'INFO' );

        if ((!$user || !isset($user['openid'])) && !empty($openid)) {
            //$user = array( 'openid' => $openid );
            //trace( $_POST, 'share session', 'SQL' );
        }
        if (!$is_follow) {
            $result['error'] = 302;//用户状态没有发生变化
            $result['msg'] = '投票需要关注';
            $result['img'] = '';
            if (!empty($wxuser['headerpic'])) {
                $result['img'] = "<img alt='' src='http://" . $_SERVER['SERVER_NAME'] . $wxuser['headerpic'] . "' width='200px;'>";
            }

            $this->ajaxReturn($result);
        }

        if ($is_follow < 0) {
            $result['error'] = 303;//服务器繁忙--
            $result['msg'] = '服务器繁忙请稍后';
            return $this->ajaxReturn($result);
        }

        $datas = array(
            "openid" => $user['openid'],
            "time" => time(),
            "updated_time" => time(),
            "ip" => get_client_ip(),
            "pid" => $pid,
            "praise" => $praise['id']
        );

        $check = RuleService::is_good($praise['id'], $user['openid'], $datas);//投票规则验证
        if ($check == 400) {
            return $this->ajaxReturn(array("error" => 400, "msg" => "活动不存在"));//活动不存在
        }
        if ($check == 401) {
            return $this->ajaxReturn(array("error" => 401, "msg" => "活动还未开始"));//活动还没开始
        }
        if ($check == 402) {
            return $this->ajaxReturn(array("error" => 402, "msg" => "活动已经结束"));//活动已经结束
        }

        if ($check['error'] == 0) {
            $result['check'] = $check;
            $mode = D("praiseItemRecord");
            $check['updated'] = $mode->insert($datas);
        } else {
            $result['error'] = 201;//验证出错
            $result['check'] = $check;
            $this->ajaxReturn($result);
        }

        if (isset($check['updated']) && $check['updated']['success']) {
            $result['error'] = 200;
            $result['updated'] = $check['updated'];
            $result['msg'] = '投票成功';
        } else {
            $result['error'] = 202;//数据库访问出错
            $result['msg'] = '投票失败';
        }

        $this->ajaxReturn($result);
    }

    public function eatadd()//TODO: could be better
    {
        $result = array('error' => 1, 'msg' => '投票成功');
        $session_time = session("mt_time");
        if ($session_time != null && ($session_time + 2) > time()) {
            $result['error'] = 302;//用户状态没有发生变化
            $result['msg'] = "操作过于频繁，请稍后重试！";
            $result['img'] = '';
            return $this->ajaxReturn($result);
        }
        session("mt_time", time());
        $pid = I("post.item", -1, "");
        $praise_id = I("post.praise", -1, "");
        $openid = I('post.wecha_id', '', '');
        $datas['pid'] = $pid;
        $datas['praise'] = $praise_id;
        $mode = D("praiseItemRecord");
        $check['updated'] = $mode->eatinsert($datas);

        $this->ajaxReturn($result);
    }
}
